package com.example.intelligentattendancesystem.mapping;

import com.example.intelligentattendancesystem.pojo.History;
import com.example.intelligentattendancesystem.pojo.Leave;
import com.example.intelligentattendancesystem.pojo.User;
import com.example.intelligentattendancesystem.pojo.Work;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WorkMapping {
    /**
     * 新增用户工作表
     *
     * @param work 工作表
     * @return 修改的条数
     */
    @Insert("insert into t_work values(#{id}, #{userId}, #{startTime},#{endTime},#{week})")
    int insertWork(Work work);

    /**
     * 通过ID查询用户的工作表
     *
     * @param userId
     * @return
     */
    @Select("select id,user_id as userId , start_time as startTime , end_time as endTime ,week from t_work where user_id = #{userId} " +
            "order by start_time")
    List<Work> selectWorkByUserId(int userId);

    /**
     * 通过ID和开始时间，结束时间，week查询
     * @param userId
     * @param start
     * @param end
     * @param week
     * @return
     */
    @Select("select id,user_id as userId , start_time as startTime , end_time as endTime ,week from t_work where user_id = #{userId} and start_time = #{start} and end_time = #{end} and week=#{week}" +
            "order by start_time")
    Work selectWorkByInfo(int userId,String start,String end,String week);

    /**
     * 查询该时刻表格中的用户id
     *
     * @param work 时刻表
     * @return 用户id列表
     */
    @Select("select user_id as userId from t_work where start_time=#{startTime} and week=#{week}")
    List<Integer> selectUserIdByWork(Work work);


    /**
     * 删除该时刻表格中的用户
     *
     * @param work
     * @return 修改的条数
     */
    @Delete("delete from t_work where user_id = #{userId} and start_time=#{startTime} and end_time=#{endTime} and week=#{week}")
    int deleteUserInWork(Work work);

    /**
     * 删除该用户的所有工作表
     *
     * @param userId
     * @return 修改的条数
     */
    @Delete("delete from t_work where user_id = #{userId}")
    int deleteUserAllWork(int userId);


    /**
     * 查询所有用户的工作时刻表
     *
     * @return
     */
    @Select("select id,user_id as userId , start_time as startTime , end_time as endTime ,week from t_work order by startTime asc")
    List<Work> selectAllWork();

    /**
     * 更新工作表，通过id
     * @param preWork
     * @return
     */
    @Update("UPDATE t_work " +
            "SET start_time=#{newWork.startTime} , end_time = #{newWork.endTime} " +
            "WHERE start_time = #{preWork.startTime}")
    int updateWork(Work preWork,Work newWork);
    /**
     * 更新工作表，通过id
     * @param preWork
     * @return
     */
    @Update("UPDATE t_work " +
            "SET user_id=#{newWork.userId}" +
            " WHERE start_time = #{preWork.startTime} and user_id = #{preWork.userId} and week = #{preWork.week}")
    int updateWorkByPre(Work preWork,Work newWork);

    @Delete("delete from t_work " +
            "where start_time=#{startTime}")
    int deleteTimeTable(Work work);

    @Select("select id,user_id as userId , start_time as startTime , end_time as endTime ,week from t_work where user_id = #{userId} and end_time = #{signOutTime} and week = #{s}")
    Work selectWorkByUserIdAndEndTeimeAndWeek(String userId, String signOutTime, String s);
}
